//
//  SearchViewController.m
//  Cartoon
//
//  Created by dllo on 15/11/3.
//  Copyright © 2015年 YSZ. All rights reserved.
//

#import "SearchViewController.h"
#import "MoreTableViewCell.h"
#import "Recommend.h"
#import "CartoonDetailsViewController.h"
@interface SearchViewController ()<UITableViewDataSource,UITableViewDelegate,UISearchBarDelegate>
@property(nonatomic,retain)UITableView *table;
@property (nonatomic ,retain) UISearchBar *searchBar;
@property(nonatomic,retain) NSString *searchStr;
@property(nonatomic,retain) NSMutableArray *searchArr;
@property(nonatomic,retain) UILabel *myLable;
@property(nonatomic,retain) UIImageView * myImageView;

@property(nonatomic,retain) UIImageView *placeholder;@end

@implementation SearchViewController




- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.tabBarController.tabBar.hidden = YES;
    self.table = [[UITableView alloc]initWithFrame:CGRectMake(0, 64, SelfViewWidth, SelfViewHeight  - 64) style:UITableViewStylePlain];
    self.table.delegate = self;
    self.table.dataSource = self;
    [self.view addSubview:self.table];
    
    
    self.searchBar= [[UISearchBar alloc] initWithFrame:CGRectMake(- 80, 24,self.view.bounds.size.width/3, 30)];
    
    self.searchBar.placeholder = @"请输入漫画名/作者";
    self.searchBar.backgroundColor = [UIColor clearColor];
    [self.searchBar setKeyboardType:UIKeyboardTypeEmailAddress];// 设置键盘样式
    self.searchBar.delegate = self;
    self.navigationItem.titleView=self.searchBar;
    
    
    [self.table registerClass:[MoreTableViewCell class] forCellReuseIdentifier:@"reuse"];
    

    self.placeholder = [[UIImageView alloc]initWithFrame:self.view.bounds];
   
    [self.view addSubview:self.placeholder];
    
    
    
    // Do any additional setup after loading the view.
}

- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar
{
    
    [self.searchBar resignFirstResponder];
    searchBar.showsCancelButton = YES;
    for(id cc in [searchBar subviews])
    {
        for (id zz in [cc subviews]) {
            if([zz isKindOfClass:[UIButton class]])
            {
                UIButton *btn = (UIButton *)zz;
                [btn setTitle:@"取消"  forState:UIControlStateNormal];
            }
        }
    }
}

- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar
{
    [self.searchBar setShowsCancelButton:YES animated:YES];
    
    self.placeholder.hidden = YES;
    
    return YES;
}


// 取消按钮被按下时,执行的方法
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
{
    [self.searchBar resignFirstResponder];
    [self.searchBar setShowsCancelButton:NO animated:YES];
    [self.navigationController popViewControllerAnimated:YES];
    
}

// 键盘中,搜索按钮被按下,执行的方法
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
    
    [self.searchBar resignFirstResponder];
    self.searchStr = searchBar.text;

    [self handlData];
    
}

- (NSMutableAttributedString *)stringChangeMutableAttributedString:(NSString *)longString otherString:(NSString *)shortString
{
    
    NSRange range = [longString rangeOfString:shortString];
    NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:longString];
    [str addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:range];
    
    return str;
}
- (void) handlData
{
    
    NSString *str = [NSString stringWithFormat:@"http://mobilev3.ac.qq.com/Search/comicSearchListForIos/key_word/%@/listcnt/12/page/1/clientType/0/local_version/2.4.0",self.searchStr];
    
   [NetHandler dataWithUrl:str complant:^(id data) {
      
       self.searchArr = [data objectForKey:@"data"];
       
       if (self.searchArr.count == 0) {
           self.table.hidden = YES;
           
           self.view.backgroundColor = [UIColor whiteColor];
           CGFloat width = self.view.bounds.size.width;
           CGFloat height = self.view.bounds.size.height;
           
           self.myLable = [[UILabel alloc]initWithFrame:CGRectMake(width/ 2 - 50 , height/ 2, 150,20 )];
           self.myLable.text = @"没有相关数据";
           [self.view addSubview:self.myLable];
          
           
           self.myImageView = [[UIImageView alloc]initWithFrame:CGRectMake(width/2 - 80, height/ 2 - 2, 25, 25)];
           self.myImageView.image = [UIImage imageNamed:@"出错了.jpg"];
           [self.view addSubview:self.myImageView];
           
           
       }else{
           self.myLable.hidden = YES;
           self.myImageView.hidden = YES;
           self.table.hidden = NO;
       }
       
         [_table reloadData];
       
       

   }];
    
    
}


-(NSInteger )tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.searchArr.count;
    
}

-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    MoreTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuse"];
    
    Recommend *cartoonList = [[Recommend alloc]init];
    [cartoonList setValuesForKeysWithDictionary:[self.searchArr objectAtIndex:indexPath.row]];
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    [cell.bigImageView sd_setImageWithURL:[NSURL URLWithString:cartoonList.cover_url]placeholderImage:[UIImage imageNamed:@"5"]];
    
    cell.namelabel.text = cartoonList.artist_name;
   
    cell.titleLabel.attributedText = [self stringChangeMutableAttributedString:cartoonList.title otherString:self.searchStr];
   
    cell.lastupLabel.text = cartoonList.lastup;
    cell.starView.starNum = cartoonList.grade;
    return cell;
    
    
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    return 120;
    
    
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    
    
    CartoonDetailsViewController *cartoon = [[CartoonDetailsViewController alloc]init];
    
    Recommend *cartoonList = [[Recommend alloc]init];
    [cartoonList setValuesForKeysWithDictionary:[self.searchArr objectAtIndex:indexPath.row]];
    
    cartoon.idtext = cartoonList.comic_id;
//    cartoon.lastNum = [cartoonList.lastup integerValue];
    
    [self.navigationController pushViewController:cartoon animated:YES];
    
    
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
